package com.chenfan.process.entity.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.ccp.entity.BaseEntity;
import com.chenfan.ccp.plug.mybatis.annotation.Column;
import com.chenfan.process.common.constants.ProcessConstants;
import com.chenfan.process.entity.vo.ProcessVo;
import com.chenfan.process.enums.ProcessNodeEnums;
import lombok.*;

import java.util.Date;
import java.util.Objects;

/**
 * @author K
 * @date 2021/3/9 审批节点信息
 */
@EqualsAndHashCode(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@TableName(value = "chenfan_paas_process_po")
public class ProcessPo extends BaseEntity<ProcessPo> {

    public static final String LINK = ",";

    @Column(commit = "节点名称")
    private String processTitle;

    @Column(commit = "节点标签")
    private String processTag;

    @Column(commit = "关联人的组织Id,多个组织ID利用,分开")
    private String associateGroupId;

    @Column(commit = "关联人的名称，用名称利用,分开")
    private String associateGroupName;

    @Column(commit = "执行时间")
    private Date executorTime;

    @Column(length = 1024, commit = "执行的入参参数")
    private String inputParam;

//    @Column(length = 1024, commit = "执行的方法")
//    private String function;

    @Column(length = 512, commit = "执行的输出参数")
    private String outputParam;

    /**
     * 参考{@link com.chenfan.process.enums.ProcessNotifyTypeEnums}
     */
    @Column(length = 4, commit = "执行的通知类型,参考{@link com.chenfan.process.enums.ProcessNotifyType}")
    private Integer notifyType;

    @Column(length = 512, commit = "执行的通知参数")
    private String notifyParam;

    /**
     * 方法的名称，url为get/post，mq可不填写
     */
    @Column(length = 512, commit = "执行的通知方式")
    private String notifyMethod;

    /**
     * 地址的名称 url为对的方法的名称，mq对应topic
     */
    @Column(length = 512, commit = "执行的通知地址")
    private String notifyAddr;

    /**
     * 执行的参数
     */
    @Column(length = 52, commit = "关联的Id")
    private String processRandomId;

    /**
     * 参考{@link com.chenfan.process.enums.ProcessNotifySourceTypeEnums}
     */
    @Column(length = 4, commit = "执行的来源,参考{@link com.chenfan.process.enums.ProcessNotifySourceType}")
    private Integer notifySource;
    /**
     * 分组id
     */
    private Long groupId;

    public boolean isDepartmentHeader() {
        return Objects.equals(this.getAssociateGroupId(), ProcessConstants.DEFAULT_SKIP_CHECK_PERMISSION_USER_ID);
    }

    public boolean isDynamic() {
        return ProcessConstants.DEFAULT_SKIP_CHECK_PERMISSION_USER_ID.equals(associateGroupId)
            || ProcessConstants.NOT_SPECIFIC_NODE.equals(associateGroupId)
            || ProcessConstants.START_APPROVAL_NODE.equals(associateGroupId);
    }

    public boolean approvalName() {
        return isDynamic() || isCommitter() || isLast();
    }

    public boolean isCommitter() {
        return Objects.equals(this.getAssociateGroupId(), "0");
    }

    public boolean isApproval() {
        return ProcessNodeEnums.APPROVAL_CREATE_PERSON.getCode().toString().equals(associateGroupId);
    }

    public ProcessVo copyProcessVo() {
        ProcessVo vo = new ProcessVo();
        org.springframework.beans.BeanUtils.copyProperties(this, vo);
        return vo;
    }

    public ProcessPo cloneProcess() {
        ProcessPo processPo = new ProcessPo();
        org.springframework.beans.BeanUtils.copyProperties(this, processPo);
        processPo.setCreateTime(new Date());
        processPo.setId(null);
        return processPo;
    }

    public boolean isLast() {
        return Objects.equals(ProcessNodeEnums.EMPTY_NODE.getCode().toString(), getAssociateGroupId());
    }
}
